package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.annottation.LogAnnottion;
import com.baizhi.cmfz.entity.Consumer;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.service.ConsumerService;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @RequestMapping("/selectByPageConsumer")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询普通用户")
    @AddCacheAnnotation
    public Map selectByPageConsumer(Integer page, Integer limit, String name) {

        return consumerService.selectByPageConsumer(page, limit, name);
    }

    @ResponseBody
    @RequestMapping("/insertConsumer")
    @LogAnnottion(type = "insert", value = "添加普通用户")
    @DeleteCacheAnnotation
    public Map insertUser(Consumer consumer, MultipartFile file, HttpServletRequest request) throws IOException {
        if (file != null) {
            //获取绝对路径
            String realPath = request.getServletContext().getRealPath("/img");
            String uuid = UUID.randomUUID().toString();
            //生成 新的文件名
            String newName = uuid + "_" + file.getOriginalFilename();
            System.out.println(newName);
            //通过输出流将文件写入到服务器磁盘
            file.transferTo(new File(realPath + "/" + newName));
            consumer.setUserImage(newName);
        }
        consumer.setUserCreateDate(new Date());
        return consumerService.insertConsumer(consumer);
    }

    @RequestMapping("/updateConsumer")
    @ResponseBody
    @LogAnnottion(type = "update", value = "修改普通用户")
    @DeleteCacheAnnotation
    public Map updateConsumer(HttpServletRequest request, Consumer consumer, MultipartFile file) throws IOException {

        if (file != null) {
            String realPath = request.getServletContext().getRealPath("/img");
            String uuid = UUID.randomUUID().toString();
            String newName = uuid + "_" + file.getOriginalFilename();
            file.transferTo(new File(realPath + "/" + newName));
            consumer.setUserImage(newName);
        }
        System.out.println(consumer + "======================");

        return consumerService.updateConsumer(consumer);
    }

    @ResponseBody
    @RequestMapping("/selectOneConsumer")
    @LogAnnottion(type = "select", value = "查询一个普通同户")
    @AddCacheAnnotation
    public Consumer selectOneConsumer(Integer id) {
        return consumerService.selectOneConsumer(id);
    }

    @RequestMapping("/deleteConsumer")
    @ResponseBody
    @LogAnnottion(type = "delet", value = "删除一个普通用户")
    @DeleteCacheAnnotation
    public Map deleteConsumer(Integer id) {

        return consumerService.deleteConsumer(id);
    }

    @ResponseBody
    @RequestMapping("/updateFreeze")
    @LogAnnottion(type = "update", value = "冻结用户")
    //修改为冻结
    @DeleteCacheAnnotation
    public Map updateFreeze(Integer id) {

       return consumerService.updateFreeze(id);
    }
    @RequestMapping("/selectNumberConsumer")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询用户数量及地区")
    @AddCacheAnnotation
    public List<Map> selectNumberConsumer(){

        return  consumerService.selectNumberConsumer();
    };

    @RequestMapping("/selectNumber")
    @ResponseBody
    @LogAnnottion(type = "select", value = "男女数量")
    @AddCacheAnnotation
    public Map selectNumber(){

        return  consumerService.selectNumber();
    };
    @ResponseBody
    @RequestMapping("/selectByDayCount")
    @LogAnnottion(type = "select", value = "查询三周的用户注册")
    @AddCacheAnnotation
    public  Map selectByDayCount(){

        return  consumerService.selectByDay();
    }

    @ResponseBody
    @RequestMapping("/selectAllOrder")
    @LogAnnottion(type = "select", value = "测试查询用户数据分析")
    @AddCacheAnnotation
    public  Map selectAllOrder(){
//279   94

//        70ms
//        75ms
//        65ms
//        64ms
     Map map = consumerService.selectAllOrder();
        return  map;
    }

    @RequestMapping("/selectOrderAll")
    @ResponseBody
    @LogAnnottion(type = "select", value = "查询用户信息分析优化后")
    @AddCacheAnnotation
    public  Map selectOrderAll(){
//        83ms
//        58ms
//        53ms
//        54ms
        return  consumerService.selectOrderAll();
    }



}
